<?php

namespace App\Listeners\Comment;

use App\Consts\GlobalMessageConst;
use App\Events\CreateCommentEvent;
use App\Listeners\BaseListener;

class CreateCommentListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CreateCommentEvent $event
     * @return bool
     */
    public function handle(CreateCommentEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);

        $comment = $event->comment;

        // 修改【评论】是否为主体发布者发布
        service()->CommentService->changeHaveAuthorReplyByQueue([
            'comment_id' => $comment['comment_id'],
            'user_id' => $comment['user_id'],
            'target_id' => $comment['target_id'],
            'target_type' => $comment['target_type']
        ]);


        $msgData = $event->commentMessageLoadData;
        if ($msgData['sender_id'] != $msgData['receive_id']) {
            // 记录消息（包括静态化）
            service()->MessageService->saveAndStaticCommentMessage($msgData);

            // 修改评论接收者的未读消息数
            repository()->MessageCountRepository->getUserMsgCountByUserIdAndMsgType($event->commentMessageLoadData['receive_id'], GlobalMessageConst::COUNT_TYPE_COMMENT);
        }

        // 记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        return true;
    }
}
